package de.bastian.hinterleitner.tools;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import de.bastian.hinterleitner.database.DataHelper;
import de.hijacksoft.wyc.R;

public class Helper {
	/**
	 * compares two Strings and returns the similarity (0 = not similar, 1 =
	 * same)
	 * 
	 * @author http://www.codeproject.com/KB/java/JavaFuzzyStringMatch.aspx
	 */
	public static double similarity(String string1, String string2) {
		// turn first string into a bigram
		ArrayList<char[]> bigram1 = new ArrayList<char[]>();
		for (int i = 0; i < string1.length() - 1; i++) {
			char[] chars = new char[2];
			chars[0] = string1.charAt(i);
			chars[1] = string1.charAt(i + 1);
			bigram1.add(chars);
		}
		// turn second string into a bigram
		ArrayList<char[]> bigram2 = new ArrayList<char[]>();
		for (int i = 0; i < string2.length() - 1; i++) {
			char[] chars = new char[2];
			chars[0] = string2.charAt(i);
			chars[1] = string2.charAt(i + 1);
			bigram2.add(chars);
		}
		// compare them
		List<char[]> copy = new ArrayList<char[]>(bigram2);
		int matches = 0;
		for (int i = bigram1.size(); --i >= 0;) {
			char[] bigram = bigram1.get(i);
			for (int j = copy.size(); --j >= 0;) {
				char[] toMatch = copy.get(j);
				if (bigram[0] == toMatch[0] && bigram[1] == toMatch[1]) {
					copy.remove(j);
					matches += 2;
					break;
				}
			}
		}
		return (double) matches / (bigram1.size() + bigram2.size());
	}

	public static double getThisMonthsTotal(Context context) {
		double total = 0;
		ArrayList<Entry> entries = getThisMonthsItems(context);

		for (Entry e : entries) {
			total += e
					.getValueAs(context.getString(R.string.currency), context);
		}
		return total;
	}

	private static ArrayList<Entry> getThisMonthsItems(Context context) {
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		int day = sp.getInt("starting", 1);
		DataHelper dbHelper = DataHelper.getDataHelper(context);
		ArrayList<Entry> entries = dbHelper.getAllEntriesAt(Calendar
				.getInstance().get(Calendar.YEAR),
				Calendar.getInstance().get(Calendar.MONTH) + 1);
		int startMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int otherMonth = startMonth;
		if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) >= day) {
			otherMonth++;
		} else {
			startMonth--;
			otherMonth--;
		}
		entries.addAll(dbHelper.getAllEntriesAt(
				Calendar.getInstance().get(Calendar.YEAR), otherMonth));
		filter(entries, startMonth, context);
		return entries;
	}

	private static void filter(ArrayList<Entry> entries, int startMonth,
			Context context) {
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		int day = sp.getInt("starting", 1);
		for (int i = 0; i < entries.size(); i++) {
			Entry e = entries.get(i);
			if (e.getMonth() == startMonth) {
				if (e.getDay() < day) {
					entries.remove(e);
					i--;
				}
			} else {
				if (e.getDay() >= day) {
					entries.remove(e);
					i--;
				}
			}
		}
	}

	public static double getWhatsLeft(double spent, Context context) {
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		double budget = sp.getFloat("budget", 200);
		int start = sp.getInt("starting", 1);
		double whatsleft = budget - spent;
		int days = start;
		if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) >= start) {
			days += Calendar.getInstance().getActualMaximum(
					Calendar.DAY_OF_MONTH)
					- Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		} else {
			days -= Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		}
		return roundToDecimals(2, whatsleft / (days + 1));
	}

	public static final double roundToDecimals(int decimals, double src) {
		return Math.round( (src * Math.pow(10, decimals)) )/ Math.pow(10, decimals);
	}

	public static String shorten(double num) {
		String amount = "";
		int total = (int) num;
		if (total >= 10000) {
			total /= 1000;
			amount = "k";
		}
		if (total >= 10000) {
			total /= 1000;
			amount = "m";
		}
		if (total >= 10000) {
			total /= 1000;
			amount = "g";
		}
		if (total >= 10000) {
			total /= 1000;
			amount = "t";
		}
		return total+amount;
	}

	public static String getValString(double val) {
		String extra = "";
		if(val*10==(int)(val*10)){
			extra += "0";
		}
		return val+extra;
	}

}
